#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 20 14:27:01 2023

@author: jcfq2
"""
# import numpy as np
# import matplotlib.pyplot as plt
import sys
# import os

sys.path.insert(0,              '../../tss')
from basic.polar_rotation import rotatePole



# cml=190


# subsolarlat=0
# subsolarlongitude=cml#240.3#+180


# s_ssl=str(int(subsolarlongitude/10+1)*10)

# # print(s_ssl)

# sys.path.insert(0,              '../planetary_values')

# v = open(os.path.join(sys.path[0],'fieldline_tracing/fieldline_tracing_mapping_contours_kk2009ext_jrm09int_north_sslong'+s_ssl+'.txt'))

# north_plotting = np.zeros([28,36,4])

# header = v.readline()

# for z in range(28):
#     for y in range(36):
#         line = v.readline()
#         north_plotting[z,y,0]=line[0:12]
#         north_plotting[z,y,1]=line[14:20]
#         null1=line[21:29]
#         if any(chr.isdigit() for chr in null1):
#             north_plotting[z,y,2]=null1
#         else:
#             north_plotting[z,y,2]=np.nan
#         null2=line[30:38]
#         if any(chr.isdigit() for chr in null2):
#             north_plotting[z,y,3]=null2
#         else:
#             north_plotting[z,y,3]=np.nan


# lat0=north_plotting[0,:,2]
# lon0=north_plotting[0,:,3]

# lat0=np.append(lat0,75)
# lon0=np.append(lon0,185)

# plt.figure()
# plt.plot(lon0,lat0)


def to_magnetic(lat0,lon0,maglong=185,maglat=75):

    # lat0_shift1,              lon0_shift1,              radius = rotatePole(lat0,              lon0,1,angle=180-maglong,              axis=[0,0,1])
    # lat0_shift2,              lon0_shift2,              radius = rotatePole(lat0_shift1,              lon0_shift1,1,angle=maglat-90,              axis=[0,1,0])

    lat0_shift1,              lon0_shift1,              radius = rotatePole(lat0,              lon0,1,angle=maglat-90,              axis=[0,1,0])
    lat0_shift2,              lon0_shift2,              radius = rotatePole(lat0_shift1,              lon0_shift1,1,angle=180-maglong,              axis=[0,0,1])
    return lat0_shift2,lon0_shift2


# lat0_shift,              lon0_shift=to_magnetic(lat0,lon0,maglong=185,maglat=75)
# plt.figure()
# plt.plot(lon0_shift,lat0_shift)

def from_magnetic(lat00,lon00,maglong=185,maglat=75):

    lat0_shift1,              lon0_shift1,              radius = rotatePole(lat00,              lon00,1,angle=90-maglat,              axis=[0,1,0])
    # lat0_shift2,              lon0_shift2,              radius = rotatePole(lat0_shift1,              lon0_shift1,1,angle=maglong-180,              axis=[0,0,1])
    # lat0_shift2,              lon0_shift2,              radius = rotatePole(lat00,              lon00,1,angle=9,              axis=[1,0,0])
    lat0_shift2,              lon0_shift2,              radius = rotatePole(lat0_shift1,              lon0_shift1,1,angle=maglong-180,              axis=[0,0,1])
    return lat0_shift2,              lon0_shift2



# lat0_shift_x,              lon0_shift_x=from_magnetic(lat0_shift,              lon0_shift)
# plt.figure()
# plt.plot(lon0_shift_x,              lat0_shift_x)


# plt.figure()
# plt.plot(lon0_shift_x,lat0_shift_x)


